#include "flow_inspector.h"
#include "muduo/base/AsyncLogging.h"

muduo::AsyncLogging* g_asyncLog = nullptr;
off_t kRollSize = 500*1000*1000;

void asyncOutput(const char* msg, int len)
{
  g_asyncLog->append(msg, len);
}

int main(int argc, const char **argv)
{
  // TODO: reading from online configure paramter
  if (argc > 2) {
    if (-1 == daemon(1, 0))
    {
      fprintf(stderr, "daemon error\n");
      exit(-1);
    }

    char name[256] = { '\0' };
    strncpy(name, argv[0], sizeof name - 1);
    muduo::AsyncLogging log(::basename(name), kRollSize);
    log.start();
    g_asyncLog = &log;
    muduo::Logger::setOutput(asyncOutput);

    EventLoop loop;
    InetAddress addr(argv[1], atoi(argv[2]));
    FlowInspector inspector(&loop, addr);
    inspector.connect();
    loop.loop();
  } else {
    printf("Usage: %s host_ip port\n", argv[0]);
  }

  return 0;
}

